package com.service.impl;

import cn.hutool.core.date.*;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.utils.PageUtils;
import com.utils.Query;


import com.dao.YimiaoxinxiDao;
import com.entity.YimiaoxinxiEntity;
import com.service.YimiaoxinxiService;
import com.entity.vo.YimiaoxinxiVO;
import com.entity.view.YimiaoxinxiView;

@Service("yimiaoxinxiService")
public class YimiaoxinxiServiceImpl extends ServiceImpl<YimiaoxinxiDao, YimiaoxinxiEntity> implements YimiaoxinxiService {
	

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<YimiaoxinxiEntity> page = this.selectPage(
                new Query<YimiaoxinxiEntity>(params).getPage(),
                new EntityWrapper<YimiaoxinxiEntity>()
        );
        return new PageUtils(page);
    }
    
    @Override
	public PageUtils queryPage(Map<String, Object> params, Wrapper<YimiaoxinxiEntity> wrapper) {
		  Page<YimiaoxinxiView> page =new Query<YimiaoxinxiView>(params).getPage();
	        page.setRecords(baseMapper.selectListView(page,wrapper));
	    	PageUtils pageUtil = new PageUtils(page);
	    	return pageUtil;
 	}


	@Override
	public List<YimiaoxinxiVO> selectListVO(Wrapper<YimiaoxinxiEntity> wrapper) {
 		return baseMapper.selectListVO(wrapper);
	}
	
	@Override
	public YimiaoxinxiVO selectVO(Wrapper<YimiaoxinxiEntity> wrapper) {
 		return baseMapper.selectVO(wrapper);
	}
	
	@Override
	public List<YimiaoxinxiView> selectListView(Wrapper<YimiaoxinxiEntity> wrapper) {
		return baseMapper.selectListView(wrapper);
	}

	@Override
	public YimiaoxinxiView selectView(Wrapper<YimiaoxinxiEntity> wrapper) {
		return baseMapper.selectView(wrapper);
	}

	@Override
	public Map<String, Object> dateChuRuKu() {
		Map<String,Object> map = new HashMap<>();
		List<String> dates = new ArrayList<>();//当前月所有日期
		//获取当前月当前日期
		Date nowDate = new Date();
		//获取当前月的第一天
		Date firstDay = DateUtil.beginOfMonth(nowDate);
		List<DateTime> dateTimes = DateUtil.rangeToList(firstDay, nowDate, DateField.DAY_OF_MONTH);
		dateTimes.forEach(dateTime ->
				dates.add(	DateUtil.format(dateTime,DatePattern.NORM_DATE_PATTERN))
		);

		System.out.println(JSON.toJSON(dateTimes));
		List<Double>  rukushuliang = new ArrayList<>();//返给前台的入库数据
		List<Double> chukushuliang = new ArrayList<>();//返给前台的出库数

		List<Map<String, String>> chukus = baseMapper.selectDateChuku();//出库数据
		List<Map<String, String>> rukus = baseMapper.selectDateRuku();//入库数据


		Map<String, Map> chuMap = new HashMap<>();
		Map<String, Map> ruMap = new HashMap<>();

		for (Map mapChuku:chukus) {
			chuMap.put((String)mapChuku.get("fdate"),mapChuku);
		}
		for (Map mapRuku:rukus) {
			ruMap.put((String)mapRuku.get("fdate"),mapRuku);
		}
		/** riqi   1     2    3     4     5    6-----27
		 *  chuku   5          4    2           4
  		 */



		//根据日期从小到大取数，
		for (String dateStr:dates ) {				
			if(chuMap.containsKey(dateStr)){
				Map m = chuMap.get(dateStr);
				chukushuliang.add((Double)m.get("shuliang"));
			}else{
				chukushuliang.add(0d);
			}

			if(ruMap.containsKey(dateStr)){
				Map m = ruMap.get(dateStr);
				rukushuliang.add((Double)m.get("shuliang"));
			}else{
				rukushuliang.add(0d);
			}
		}
		map.put("dates",dates);

		map.put("chuku",chukushuliang);
		map.put("ruku",rukushuliang);



		return map;
	}

	public static void main(String[] args) {
		//获取当前月当前日期
		Date nowDate = new Date();
		//获取当前月的第一天
		Date firstDay = DateUtil.beginOfMonth(nowDate);
		List<DateTime> dateTimes = DateUtil.rangeToList(firstDay, nowDate, DateField.DAY_OF_MONTH);
		System.out.println(DateUtil.format(dateTimes.get(0), DatePattern.NORM_DATE_PATTERN));
		System.out.println(DateUtil.format(dateTimes.get(dateTimes.size()-1), DatePattern.NORM_DATE_PATTERN));
	}
}
